#include "curveseditorwindow.h"
#include "ui_curveseditorwindow.h"
#include "icons.h"

#include <QDebug>

namespace CurveEditor {

CurvesEditorWindow::CurvesEditorWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::CurvesEditorWindow)
{
    ui->setupUi(this);

    createActions();
    createMenus();
    createToolBars();
    createStatusBar();

    manageActions();

    // Pages widget
    m_projectPages = new QTabWidget();
    m_projectPages->setTabsClosable(true);
    m_projectPages->setMovable(true);

    connect(m_projectPages, &QTabWidget::currentChanged, this, &CurvesEditorWindow::tabCurrentChanged);
    connect(m_projectPages, &QTabWidget::tabCloseRequested, this, &CurvesEditorWindow::tabCloseRequested);
    connect(m_projectPages, &QTabWidget::tabBarDoubleClicked, this, &CurvesEditorWindow::tabBarDoubleClicked);

    QWidget *centralWidget = new QWidget();
    auto *hLayout = new QHBoxLayout();
    hLayout->setContentsMargins(0, 0, 0, 0);
    hLayout->addWidget(m_projectPages);
    centralWidget->setLayout(hLayout);
    this->setCentralWidget(centralWidget);

    showProjectTitle();
}

CurvesEditorWindow::~CurvesEditorWindow()
{
    delete ui;
}

void CurvesEditorWindow::closeEvent(QCloseEvent *event)
{

}

void CurvesEditorWindow::createActions()
{
    /* File menu */
    fileNewAction = new QAction(tr("&New..."), this);
    fileNewAction->setIcon(Icons::FileNew());
    fileNewAction->setShortcut(QKeySequence::New);
    fileNewAction->setStatusTip(tr("Create a new Lines project"));
    connect(fileNewAction, SIGNAL(triggered()), this, SLOT(fileNew()));

    fileOpenAction = new QAction(tr("&Open..."), this);
    fileOpenAction->setIcon(Icons::FileOpen());
    fileOpenAction->setShortcut(QKeySequence::Open);
    fileOpenAction->setStatusTip(tr("Open an existing Lines project"));
    connect(fileOpenAction, SIGNAL(triggered()), this, SLOT(fileOpen()));

    fileSaveAction = new QAction(tr("&Save"), this);
    fileSaveAction->setIcon(Icons::FileSave());
    fileSaveAction->setShortcut(QKeySequence::Save);
    fileSaveAction->setStatusTip(tr("Save current project"));
    connect(fileSaveAction, SIGNAL(triggered()), this, SLOT(fileSave()));

    fileSaveAsAction = new QAction(tr("Save &As..."), this);
    fileSaveAsAction->setIcon(Icons::FileSaveAs());
    fileSaveAsAction->setShortcut(QKeySequence::SaveAs);
    fileSaveAsAction->setStatusTip(tr("Save as a other project"));
    connect(fileSaveAsAction, SIGNAL(triggered()), this, SLOT(fileSaveAs()));

    filePrintAction = new QAction(tr("&Print") , this);
    filePrintAction->setIcon(Icons::FilePrint());
    filePrintAction->setShortcut(QKeySequence::Print);
    filePrintAction->setStatusTip(tr("Select project Print mode"));
    connect(filePrintAction, SIGNAL(triggered()), this, SLOT(fileShowPrintPage()));

    fileCloseAction = new QAction(tr("&Close"), this);
    fileCloseAction->setIcon(Icons::FileClose());
    fileCloseAction->setShortcut(QKeySequence::Close);
    fileCloseAction->setStatusTip(tr("Close current project"));
    connect(fileCloseAction, SIGNAL(triggered()), this, SLOT(fileClose()));

    fileExitAction = new QAction(tr("E&xit"), this);
    fileExitAction->setIcon(Icons::FileExit());
    fileExitAction->setShortcut(QKeySequence::Quit);
    fileExitAction->setStatusTip(tr("Exit LineEditor"));
    connect(fileExitAction, SIGNAL(triggered()), this, SLOT(fileExit()));

    /* Edit menu */
    editUndoAction = new QAction(tr("Undo"), this);
    editUndoAction->setIcon(Icons::EditUndo());
    editUndoAction->setShortcut(QKeySequence::Undo);
    editUndoAction->setStatusTip(tr("Undo"));
    connect(editUndoAction, SIGNAL(triggered()), this, SLOT(editUndo()));

    editRedoAction = new QAction(tr("Redo"), this);
    editRedoAction->setIcon(Icons::EditRedo());
    editRedoAction->setShortcut(QKeySequence::Redo);
    editRedoAction->setStatusTip(tr("Redo"));
    connect(editRedoAction, SIGNAL(triggered()), this, SLOT(editRedo()));

    editCutAction = new QAction(tr("Cut"), this);
    editCutAction->setIcon(Icons::EditCut());
    editCutAction->setShortcut(QKeySequence::Cut);
    editCutAction->setStatusTip(tr("Cut the dots"));
    connect(editCutAction, SIGNAL(triggered()), this, SLOT(editCut()));

    editCopyAction = new QAction(tr("&Copy"), this);
    editCopyAction->setIcon(Icons::EditCopy());
    editCopyAction->setShortcut(QKeySequence::Copy);
    editCopyAction->setStatusTip(tr("Copy the dots"));
    connect(editCopyAction, SIGNAL(triggered()), this, SLOT(editCopy()));

    editPasteAction = new QAction(tr("&Paste"), this);
    editPasteAction->setIcon(Icons::EditPaste());
    editPasteAction->setShortcut(QKeySequence::Paste);
    editPasteAction->setStatusTip(tr("Paste the dots"));
    connect(editPasteAction, SIGNAL(triggered()), this, SLOT(editPaste()));

    editDeleteAction = new QAction(tr("&Delete"), this);
    editDeleteAction->setIcon(QIcon::fromTheme("edit-delete"));
    editDeleteAction->setShortcut(QKeySequence::Delete);
    editDeleteAction->setStatusTip(tr("Delete the dots"));
    connect(editDeleteAction, SIGNAL(triggered()), this, SLOT(editDelete()));

    /* View actions */
    viewGridAction = new QAction(tr("Grid"), this);
    viewGridAction->setCheckable(true);
    viewGridAction->setStatusTip(tr("Show grid"));
    connect(viewGridAction, SIGNAL(toggled(bool)), this, SLOT(viewGrid(bool)));

    viewMarkupAction = new QAction(tr("Markup"), this);
    viewMarkupAction->setCheckable(true);
    viewMarkupAction->setStatusTip(tr("Show markup lines"));
    connect(viewMarkupAction, SIGNAL(toggled(bool)), this, SLOT(viewMarkup(bool)));

    viewZoomInAction = new QAction(tr("Zoom &In"), this);
    viewZoomInAction->setIcon(Icons::ZoomIn());
    viewZoomInAction->setShortcut(QKeySequence::ZoomIn);
    viewZoomInAction->setStatusTip(tr("Increase magnification"));
    connect(viewZoomInAction, SIGNAL(triggered()), this, SLOT(viewZoomIn()));

    viewZoomOutAction = new QAction(tr("Zoom &Out"), this);
    viewZoomOutAction->setIcon(Icons::ZoomOut());
    viewZoomOutAction->setShortcut(QKeySequence::ZoomOut);
    viewZoomOutAction->setStatusTip(tr("Decrease magnification"));
    connect(viewZoomOutAction, SIGNAL(triggered()), this, SLOT(viewZoomOut()));

    viewZoom1To1Action = new QAction(tr("Zoom &1 to 1"), this);
    viewZoom1To1Action->setIcon(Icons::ZoomOriginal());
    viewZoom1To1Action->setStatusTip(tr("Restore scale to 100%"));
    connect(viewZoom1To1Action, SIGNAL(triggered()), this, SLOT(viewZoom1To1()));

    viewZoomToFitAction = new QAction(tr("Zoom to &Fit"), this);
    viewZoomToFitAction->setIcon(Icons::ZoomBestFit());
    viewZoomToFitAction->setStatusTip(tr("Fit window"));
    connect(viewZoomToFitAction, SIGNAL(triggered()), this, SLOT(viewZoomToFit()));

    /* Help actions */
    helpAboutAction = new QAction(tr("&About..."), this);
    helpAboutAction->setIcon(QIcon::fromTheme("help-about"));
    helpAboutAction->setStatusTip(tr("About ..."));
    connect(helpAboutAction, SIGNAL(triggered()), this, SLOT(helpAbout()));
}

void CurvesEditorWindow::createMenus()
{
    fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(fileNewAction);
    fileMenu->addAction(fileOpenAction);
    fileMenu->addAction(fileSaveAction);
    fileMenu->addAction(fileSaveAsAction);
    fileMenu->addSeparator();
    fileMenu->addAction(filePrintAction);
    fileMenu->addSeparator();
    fileMenu->addAction(fileCloseAction);
    fileMenu->addAction(fileExitAction);

    editMenu = menuBar()->addMenu(tr("&Edit"));
    editMenu->addAction(editUndoAction);
    editMenu->addAction(editRedoAction);
    editMenu->addSeparator();
    editMenu->addAction(editCutAction);
    editMenu->addAction(editCopyAction);
    editMenu->addAction(editPasteAction);
    editMenu->addAction(editDeleteAction);
    editMenu->addSeparator();

    viewMenu = menuBar()->addMenu(tr("&View"));
    viewMenu->addAction(viewGridAction);
    viewMenu->addAction(viewMarkupAction);
    viewMenu->addSeparator();
    viewMenu->addAction(viewZoomInAction);
    viewMenu->addAction(viewZoomOutAction);
    viewMenu->addAction(viewZoom1To1Action);
    viewMenu->addAction(viewZoomToFitAction);

    helpMenu = menuBar()->addMenu(tr("&Help"));
    helpMenu->addAction(helpAboutAction);
}

void CurvesEditorWindow::createToolBars()
{
    fileToolBar = addToolBar(tr("&File"));
    fileToolBar->addAction(fileNewAction);
    fileToolBar->addAction(fileOpenAction);
    fileToolBar->addAction(fileSaveAction);
}

void CurvesEditorWindow::createStatusBar()
{

}

void CurvesEditorWindow::manageActions()
{

}

void CurvesEditorWindow::showProjectTitle()
{
    if (m_projectPages->count() < 1) {
        setWindowTitle("CurveEditor");
    } else {
        QString name = m_projectPages->tabText(m_projectPages->currentIndex());
        QString modified = "*";//() ? "*" : "";
        setWindowTitle(name + " - CurveEditor");
    }
}

void CurvesEditorWindow::fileNew()
{
    QWidget *nPage = new QWidget;

    QString name;
    QStringList tabNames;

    for (int i = 0; i < m_projectPages->count(); i++) {
        tabNames.append(m_projectPages->tabText(i));
    }
    int j = 0;
    do {
        name = QString("New %1*").arg(j++);
        if (!tabNames.contains(name)) break;
    } while (1);
    j = m_projectPages->addTab(nPage, name);
    m_projectPages->setCurrentIndex(j);

    qDebug() << nPage << m_projectPages->count() << name;
}

void CurvesEditorWindow::fileOpen()
{

}

void CurvesEditorWindow::fileSave()
{

}

void CurvesEditorWindow::fileSaveAs()
{

}

void CurvesEditorWindow::fileShowPrintPage()
{

}

void CurvesEditorWindow::fileClose()
{
    int index;
    index = m_projectPages->currentIndex();
    if (index >= 0) {
        // check if need save project
        tabCloseRequested(index);
    }
}

void CurvesEditorWindow::fileExit()
{
    // save all projects?

    // exit application
    qApp->exit(0);
}

void CurvesEditorWindow::editUndo()
{

}

void CurvesEditorWindow::editRedo()
{

}

void CurvesEditorWindow::editCut()
{

}

void CurvesEditorWindow::editCopy()
{

}

void CurvesEditorWindow::editPaste()
{

}

void CurvesEditorWindow::editDelete()
{

}

void CurvesEditorWindow::editSelectAll()
{

}

void CurvesEditorWindow::editUnSelectAll()
{

}

void CurvesEditorWindow::viewGrid(bool)
{

}

void CurvesEditorWindow::viewMarkup(bool)
{

}

void CurvesEditorWindow::viewZoomIn()
{

}

void CurvesEditorWindow::viewZoomOut()
{

}

void CurvesEditorWindow::viewZoom1To1()
{

}

void CurvesEditorWindow::viewZoomToFit()
{

}

void CurvesEditorWindow::helpAbout()
{

}

void CurvesEditorWindow::tabCurrentChanged(int index)
{

    showProjectTitle();
}

void CurvesEditorWindow::tabCloseRequested(int index)
{
    // check if need save
    QWidget *mPage = m_projectPages->widget(index);
    m_projectPages->removeTab(index);
    delete mPage;

    showProjectTitle();
}

void CurvesEditorWindow::tabBarDoubleClicked(int index)
{
    tabCloseRequested(index);
}

} // CurveEditor
